package com.batdream.message.service.impl;

import com.batdream.message.enums.MsgActionEnum;
import com.batdream.message.mapper.FriendsRequestMapper;
import com.batdream.message.mapper.UsersMapper;
import com.batdream.message.netty.ChatHandler;
import com.batdream.message.netty.UserChannelContext;
import com.batdream.message.pojo.DataContent;
import com.batdream.message.pojo.FriendsRequest;
import com.batdream.message.pojo.MyFriends;
import com.batdream.message.pojo.Users;
import com.batdream.message.service.MyFriendsService;
import com.batdream.message.service.friendsRequestService;
import com.batdream.message.utils.JSONResult;
import io.netty.channel.Channel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

@Service
public class friendsRequestServiceImpl implements friendsRequestService {

    private final FriendsRequestMapper friendsRequestMapper;
    private final UsersMapper usersMapper;
    private final MyFriendsService myFriendsService;
    private final UserChannelContext userChannelContext;

    @Autowired
    public friendsRequestServiceImpl(FriendsRequestMapper friendsRequestMapper, UsersMapper usersMapper, MyFriendsService myFriendsService, UserChannelContext userChannelContext) {
        this.friendsRequestMapper = friendsRequestMapper;
        this.usersMapper = usersMapper;
        this.myFriendsService = myFriendsService;
        this.userChannelContext = userChannelContext;
    }

    @Override
    public Integer saveRequest(FriendsRequest request) {
        return friendsRequestMapper.insert(request);
    }

    @Override
    public FriendsRequest searchrequestbyTwinsId(FriendsRequest friendsRequest) {
        return friendsRequestMapper.selectOne(friendsRequest);
    }

    @Transactional
    @Override
    public JSONResult acceptFriendRequest(String sendUserId, String acceptUserId) {
        Users sendUser = usersMapper.selectByPrimaryKey(sendUserId);
        if (Objects.isNull(sendUser)){
            return JSONResult.errorMsg("发送请求用户id错误");
        }
        Users acceptUser = usersMapper.selectByPrimaryKey(acceptUserId);
        if (Objects.isNull(acceptUser)){
            return JSONResult.errorMsg("接受请求用户id错误");
        }

        //验证是否有请求消息
        FriendsRequest friendsRequest = new FriendsRequest();
        friendsRequest.setAcceptUserId(acceptUserId);
        friendsRequest.setSendUserId(sendUserId);
        FriendsRequest request = searchrequestbyTwinsId(friendsRequest);
        if (Objects.isNull(request)){
            return JSONResult.errorMsg("未接到当前好友的添加请求");
        }

        //验证是否已经是好友
        MyFriends myFriends = new MyFriends();
        myFriends.setMyFriendUserId(sendUserId);
        myFriends.setMyUserId(acceptUserId);
        MyFriends friend = myFriendsService.searchMyfriendByUsersId(myFriends);
        if (Objects.nonNull(friend)){
            //删除请求记录
            dropRequest(request.getId());
            return JSONResult.ok();
        }

        //添加朋友记录
        Integer numberOfSuccessfulOne = myFriendsService.addFriend(sendUserId, acceptUserId);
        Integer numberOfSuccessfulTwo = myFriendsService.addFriend(acceptUserId,sendUserId);
        if (Objects.isNull(numberOfSuccessfulOne)||Objects.isNull(numberOfSuccessfulTwo)){
            throw new RuntimeException("服务器异常请稍后重试");
        }

        //删除请求记录
        dropRequest(request.getId());

        //主动发送web socket到好友请求发送者 刷新他的好友列表
        if (userChannelContext.containsKey(sendUser.getId())){
            DataContent dataContent = new DataContent();
            dataContent.setAction(MsgActionEnum.PULL_FRIEND.getType());
            Channel channel = userChannelContext.getChannel(sendUser.getId());
            ChatHandler.sendWebMsgByUserId(channel.id(), dataContent);
        }

        return JSONResult.ok();
    }

    @Override
    public JSONResult dropFriendRequest(String sendUserId, String acceptUserId) {
        Users sendUser = usersMapper.selectByPrimaryKey(sendUserId);
        if (Objects.isNull(sendUser)){
            return JSONResult.errorMsg("发送请求用户id错误");
        }
        Users acceptUser = usersMapper.selectByPrimaryKey(acceptUserId);
        if (Objects.isNull(acceptUser)){
            return JSONResult.errorMsg("接受请求用户id错误");
        }

        //验证是否有请求消息
        FriendsRequest friendsRequest = new FriendsRequest();
        friendsRequest.setAcceptUserId(acceptUserId);
        friendsRequest.setSendUserId(sendUserId);
        FriendsRequest request = searchrequestbyTwinsId(friendsRequest);
        if (Objects.isNull(request)){
            return JSONResult.errorMsg("未接到当前好友的添加请求");
        }
        Integer num = dropRequest(request.getId());
        if (num==0){
            JSONResult.errorMsg("服务器异常,请稍后重试");
        }
        return JSONResult.ok();
    }

    /**
     * 根据主键删除好友请求信息
     * @param requestId 好友请求记录的id
     */
    private Integer dropRequest(String requestId) {
        return friendsRequestMapper.deleteByPrimaryKey(requestId);
    }


}
